#include "myappointmentspage.h"

MyAppointmentsPage::MyAppointmentsPage(int currentID, QString &role, QWidget *parent) : QWidget(parent) {
    this->currentID = currentID;
    this->role = role;
    qDebug() << "======================================================";
    qDebug() << "Myappointmentspage is call";
    qDebug() << "======================================================";
    // 设置整体样式
    setStyleSheet("background-color: #f9f9f9; color: #333;"); // 设置背景为浅灰色和文字颜色

    // 创建主布局
    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->setContentsMargins(30, 20, 30, 20); // 设置间距

    // 添加标题
    QLabel *titleLabel = new QLabel("我的就诊记录", this);
    titleLabel->setAlignment(Qt::AlignCenter);
    titleLabel->setStyleSheet("font-size: 28px; font-weight: bold; color: #2C3E50;");
    layout->addWidget(titleLabel);

    // 创建就诊记录列表
    appointmentsList = new QListWidget(this);
    appointmentsList->setStyleSheet("font-size: 18px; background-color: white; border: 1px solid #ddd; border-radius: 10px; padding: 10px;");
    layout->addWidget(appointmentsList);

    // 创建按钮区
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    buttonLayout->setSpacing(20); // 设置按钮之间的间距

    // 查看详细信息按钮
    QPushButton *viewDetailsButton = new QPushButton("查看详情", this);
    viewDetailsButton->setStyleSheet("background-color: #3498db; color: white; border-radius: 8px; font-size: 16px; padding: 10px 20px;");
    connect(viewDetailsButton, &QPushButton::clicked, [this]() {
        QListWidgetItem *selectedItem = appointmentsList->currentItem();
        if (selectedItem) {
            // 查看详细信息
            QMessageBox::information(this, "详细信息", "记录详情: " + selectedItem->text());
        } else {
            QMessageBox::warning(this, "警告", "请先选择一条记录！");
        }
    });
    buttonLayout->addWidget(viewDetailsButton);

    // 取消预约按钮
    QPushButton *cancelButton = new QPushButton("取消预约", this);
    cancelButton->setStyleSheet("background-color: #e74c3c; color: white; border-radius: 8px; font-size: 16px; padding: 10px 20px;");
    connect(cancelButton, &QPushButton::clicked, [this]() {
        QListWidgetItem *selectedItem = appointmentsList->currentItem();
        if (selectedItem) {
            // 取消预约
            QMessageBox::information(this, "取消预约", "您已成功取消预约: " + selectedItem->text());
            delete selectedItem; // 删除该项
        } else {
            QMessageBox::warning(this, "警告", "请先选择一条记录！");
        }
    });
    buttonLayout->addWidget(cancelButton);

    // 返回按钮
    QPushButton *backButton = new QPushButton("返回", this);
    backButton->setStyleSheet("background-color: #2ecc71; color: white; border-radius: 8px; font-size: 16px; padding: 10px 20px;");
    connect(backButton, &QPushButton::clicked, this, [this]() {
        emit backToHomePage(); // 返回主页的信号
    });
    buttonLayout->addWidget(backButton);

    layout->addLayout(buttonLayout);

    // 设置整体布局
    setLayout(layout);
}

void MyAppointmentsPage::loadMyAppointments(int currentID) {
    qDebug() << "加载当前用户的预约数据，用户ID：" << currentID;

    // 通过 currentID 来构建 SQL 查询语句
    QString sql = QString("SELECT a.AppointmentID, a.AppointmentDate, a.Status, d.UserID AS DoctorID, d.Name AS DoctorName, doc.Department "
                          "FROM Appointments a "
                          "JOIN Users d ON a.DoctorID = d.UserID "
                          "JOIN Doctors doc ON d.UserID = doc.DoctorID "
                          "WHERE a.PatientID = %1").arg(currentID);

    // 创建 ClientTcp 对象并发送查询请求
    tcp = new ClientTcp(APPOINTMENTS_DATA);
    tcp->sendMsg(sql);  // 发送查询请求

    // 连接 readyRead 信号，以接收数据
    connect(tcp->socket, &QTcpSocket::readyRead, this, [this]() {
        // 获取服务器返回的数据
        this->appo_Data = tcp->getAppointmentsData();  // 假设 tcp->getAppointmentsData() 返回的是 QList<QJsonObject>

        // 清空现有的列表项
        appointmentsList->clear();

        // 遍历获取到的预约数据并添加到列表中
        for (const auto &appointment : appo_Data) {
            QString date = appointment.value("AppointmentDate").toString();
            QString id = appointment.value("AppointmentID").toString();
            QString doctorName = appointment.value("DoctorName").toString();
            QString status = appointment.value("Status").toString();
            QString department = appointment.value("Department").toString();

            // 拼接显示文本
            QString displayText = QString("%1: %2 - %3").arg(date).arg(department).arg(doctorName);

            // 将拼接后的文本添加到列表中
            appointmentsList->addItem(displayText);
        }
    });
}
